🐍 Python Sets Tutorial

Master Python's powerful unordered collection data structure

What is a Set?

A set is an unordered collection of unique items in Python. Sets are defined by curly braces {} and automatically eliminate duplicate values.

# Creating a set my_set = {1, 2, 3, 4, 5} print(my_set) # Output: {1, 2, 3, 4, 5} # Duplicates are automatically removed my_set = {1, 2, 2, 3, 3, 3, 4} print(my_set) # Output: {1, 2, 3, 4}
Sets are unordered, so you cannot access items by index. The order of items may vary.

Creating Sets

Method 1: Using Curly Braces

fruits = {'apple', 'banana', 'orange'} numbers = {1, 2, 3, 4, 5} mixed = {1, 'hello', 3.14, True}

Method 2: Using the set() Constructor

# From a list my_set = set([1, 2, 3, 4]) # From a string (creates set of characters) char_set = set('hello') print(char_set) # Output: {'h', 'e', 'l', 'o'} # Empty set (must use set(), not {}) empty_set = set()
To create an empty set, you must use set(). Using {} creates an empty dictionary instead!

Adding and Removing Items

Adding Items

my_set = {1, 2, 3} # Add a single item my_set.add(4) print(my_set) # Output: {1, 2, 3, 4} # Add multiple items my_set.update([5, 6, 7]) print(my_set) # Output: {1, 2, 3, 4, 5, 6, 7}

Removing Items

my_set = {1, 2, 3, 4, 5} # Remove specific item (raises error if not found) my_set.remove(3) # Discard item (no error if not found) my_set.discard(10) # No error even though 10 doesn't exist # Remove and return arbitrary item item = my_set.pop() # Clear all items my_set.clear()

Set Operations

Example Sets for Operations:
A = {1, 2, 3, 4, 5} B = {4, 5, 6, 7, 8}

Union (|)

Combines all elements from both sets.

union = A | B # or union = A.union(B) print(union) # Output: {1, 2, 3, 4, 5, 6, 7, 8}

Intersection (&)

Returns only elements present in both sets.

intersection = A & B # or intersection = A.intersection(B) print(intersection) # Output: {4, 5}

Difference (-)

Returns elements in the first set but not in the second.

difference = A - B # or difference = A.difference(B) print(difference) # Output: {1, 2, 3}

Symmetric Difference (^)

Returns elements in either set, but not in both.

sym_diff = A ^ B # or sym_diff = A.symmetric_difference(B) print(sym_diff) # Output: {1, 2, 3, 6, 7, 8}

Set Methods Reference

Method Description
add() Adds an element to the set
clear() Removes all elements from the set
copy() Returns a shallow copy of the set
difference() Returns the difference between two sets
discard() Removes an element from the set (no error if not found)
intersection() Returns the intersection of two sets
issubset() Checks if set is a subset of another
issuperset() Checks if set is a superset of another
pop() Removes and returns an arbitrary element
remove() Removes a specific element (raises error if not found)
union() Returns the union of two sets
update() Updates the set with elements from another iterable

Common Use Cases

1. Removing Duplicates

numbers = [1, 2, 2, 3, 4, 4, 5] unique_numbers = list(set(numbers)) print(unique_numbers) # Output: [1, 2, 3, 4, 5]

2. Membership Testing

valid_users = {'alice', 'bob', 'charlie'} if 'alice' in valid_users: print("Access granted!") # Fast O(1) lookup

3. Finding Common Elements

student1_courses = {'math', 'physics', 'chemistry'} student2_courses = {'biology', 'chemistry', 'math'} common_courses = student1_courses & student2_courses print(common_courses) # Output: {'math', 'chemistry'}

Set Comprehensions

Just like list comprehensions, you can create sets using comprehensions:

# Squares of even numbers squares = {x**2 for x in range(10) if x % 2 == 0} print(squares) # Output: {0, 64, 4, 36, 16} # Unique characters from a string (uppercase) unique_chars = {char.upper() for char in 'hello world'} print(unique_chars) # Output: {'H', 'E', 'L', 'O', 'W', 'R', 'D', ' '}

Frozen Sets

Frozen sets are immutable versions of sets. They can be used as dictionary keys or as elements of other sets.

# Creating a frozen set frozen = frozenset([1, 2, 3, 4]) # Can be used as dictionary keys my_dict = {frozen: 'value'} # Cannot be modified # frozen.add(5) # This would raise an error

Key Takeaways

  • Sets store unique, unordered elements
  • Great for eliminating duplicates and fast membership testing
  • Support mathematical operations like union, intersection, and difference
  • Elements must be immutable (hashable) types
  • Cannot be indexed or sliced (unordered)
  • More efficient than lists for checking membership (O(1) vs O(n))